package com.aiju.cash.ui.setting;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.ShopClerk;
import com.aiju.cash.R;
import com.aiju.cash.core.api.LoginApi;
import com.aiju.cash.core.api.ShopApi;
import com.aiju.cash.core.api.SimpleCallback;
import com.aiju.cash.ui.login.manager.LoginCheck;
import com.aiju.cash.widgit.LineEditText;
import com.aiju.common.ActivityStack;
import com.aiju.utils.CheckUtil;
import com.aiju.utils.KeyBoardUtils;
import com.aiju.utils.T;

/**
 * @author: 佐为
 * @date: 2016-12-19 14:57
 * @version: V1.0
 * @Description: 修改名称
 */
public class StoreNameActivity extends BaseTitleActivity {

    public static final int SHOW_STORE = 0;//店铺
    public static final int SHOW_USER = 1;//用户
    public static final int SHOW_CLERK = 2; //店员

    private int showType; //显示的类型
    private String showName; //显示的名称

    private ShopClerk mClerk; //店员
    private LineEditText storeNameEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_name);

        showName = getIntent().getStringExtra("showName");
        showType = getIntent().getIntExtra("showType", 0);

        storeNameEditText = (LineEditText) findViewById(R.id.ev_store_name);
        storeNameEditText.setLineColor(getResources().getColor(R.color.app_divider_color));
        storeNameEditText.setText(showName);
        storeNameEditText.setSelection(storeNameEditText.length());
        KeyBoardUtils.openKeybord(storeNameEditText, this);

        if (showType == SHOW_STORE) {
            setCenterTitle("修改店铺名称");
        } else if (showType == SHOW_USER) {
            setCenterTitle("修改昵称");
            storeNameEditText.setHint("修改个人昵称");
        } else {
            setCenterTitle("修改昵称");
            storeNameEditText.setHint("修改店员昵称");
        }
        setRightTitle("保存");
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newName = storeNameEditText.getText().toString();
                if (showName.equals(newName)) {//未改动
                    onBackPressed();
                    return;
                }

                if (showType == SHOW_USER) {
                    if (!CheckUtil.checkNickName(newName)) {
                        T.ToastShort("昵称仅支持1-15位汉字、字母、数字及下划线，请重新输入");
                        return;
                    }
                    updateUserName(mUser.getId(), newName, mUser.getStoreId());
                } else if (showType == SHOW_STORE) {
                    if (TextUtils.isEmpty(newName)) {
                        updateStoreName(mUser.getUserName());
                        T.ToastShort("已使用您的手机号作为店铺名称");
                    } else {
                        if (!LoginCheck.checkShopName(newName)) {
                            T.ToastShort("店铺名称仅支持1-15位汉字、字母、数字及下划线，请重新输入");
                            return;
                        }
                        updateStoreName(newName);
                    }
                } else {
                    if (!CheckUtil.checkNickName(newName)) {
                        T.ToastShort("昵称仅支持1-15位汉字、字母、数字及下划线，请重新输入");
                        return;
                    }
                    mClerk = (ShopClerk) getIntent().getSerializableExtra("clerk");
                    updateUserName(mClerk.getId(), newName, mUser.getStoreId());
                }
            }
        });
    }

    /**
     * 修改用户、店员昵称
     */
    private void updateUserName(String id, final String newName, String storeId) {
        mDialogHelper.showLoadingProgressDialog();
        new LoginApi().userUpdate(id, mUser.getToken(), newName, storeId, new SimpleCallback(mDialogHelper) {
            @Override
            protected void onDataSuccess(String data) {
                T.ToastShort("昵称修改成功");
                mUser.setUserName(newName);
                if (showType == SHOW_CLERK) {
                    mClerk.setUserName(newName);
                    onBackPressed();
                    ActivityStack.cleanTop(3);
                    startActivity(ClerkManagerActivity.class);
                    return;
                }
                onBackPressed();
            }

            @Override
            protected void onDataFailure(int code, String msg) {
                super.onDataFailure(code, msg);
                T.ToastShort("昵称修改失败");
            }
        });
    }

    /**
     * 修改店铺名称
     *
     * @param newName
     */
    private void updateStoreName(final String newName) {
        mDialogHelper.showLoadingProgressDialog();
        new ShopApi().updateShop(mUser.getShop().getId(), newName, new SimpleCallback(mDialogHelper) {
            @Override
            protected void onDataSuccess(String data) {
                mUser.getShop().setShopName(newName);
                onBackPressed();
                T.ToastShort("店铺名称修改成功");
            }

            @Override
            protected void onDataFailure(int code, String msg) {
                super.onDataFailure(code,msg);
                T.ToastShort("店铺名称修改失败");
            }
        });

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        KeyBoardUtils.closeKeybord(storeNameEditText, this);
    }

}
